<template>
	<view>
		<uni-nav-bar color="#333333" background-color="white" @clickLeft="showBack()" left-icon="back">
			<block v-slot:left>
				<view class="city">
					<uni-icons type="back" color="#333333" size="30" />
				</view>
			</block>
			<view style="display: flex;width: 100%;">
				<view style="display: flex;flex-direction: column;width: 45%;padding: 2px;align-items: center;">
					<view style="font-size: 30rpx;height: 80rpx;color: #666ee8;">身高</view>
					<view style="height: 5rpx;width: 100%;background-color: #666ee8;border-radius: 20rpx;"></view>
				</view>
				<view @click="gotoWeight()"
					style="display: flex;flex-direction: column;width: 45%;padding: 2px;align-items: center;">
					<view style="font-size: 30rpx;height: 80rpx;">体重</view>
				</view>
			</view>
		</uni-nav-bar>
		<view style="margin-top: 100rpx;display: flex;justify-content: center;">
			<view class="xuanxiang">
				<view class="quxian" :class="{'choose-item':chooseItem}" @click="chooseQuxian()">曲线</view>
				<view class="jilu" :class="{'choose-item':!chooseItem}" @click="chooseQuxian()">记录</view>
			</view>
		</view>

		<view class="tubiao" v-if="chooseItem">
			<view>单位：cm</view>
			<view class="charts-box">
				<qiun-data-charts type="area" :opts="opts" :chartData="chartData" />
			</view>
		</view>

		<view class="tubiao2" v-else>
			<view class="charts-box2">
				<view style="display: flex;">
					<text style="width: 50%;font-size: 28rpx;color: #bcbbc5;">年龄</text>
					<text style="width: 50%;font-size: 28rpx;color: #bcbbc5;">身高</text>
				</view>
				<view
					style="height: 1rpx;width: 100%;background-color: #bcbbc5;border-radius: 20rpx;margin-top: 30rpx;margin-bottom: 30rpx;" />

				<!-- dklfsaldnfjldslkfjdslkjfalkdsajfo;jadl -->
				<view v-for="heightItem in heightRecords" style="width: 100%;height: 100rpx;">
					<scroll-view scroll-y="true">
						<view style="display: flex;">
							<text
								style="width: 50%;font-size: 28rpx;color: #bcbbc5;">{{heightItem.cdAge?heightItem.cdAge:"未填写"}}</text>
							<text
								style="font-size: 36rpx;color: #666ee8;">{{heightItem.cdHeight?heightItem.cdHeight:"未填写"}}</text>
							<text style="font-size: 28rpx;color: #bcbbc5;">cm</text>

						</view>

						<view
							style="height: 1rpx;width: 100%;background-color: #bcbbc5;border-radius: 20rpx;margin-top: 30rpx;margin-bottom: 30rpx;" />
					</scroll-view>
				</view>
			</view>




		</view>


		<view style="margin-top: 100rpx;" @click="gotoAdd()">
			<button class="tianjia">添加记录</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				childrenId: null,
				heightRecords: [],
				pageSize: 10,
				chooseItem: true,
				myChart: null,
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					title: {
						name: "身高"
					},
					color: ["#666ee8", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 10, 0, 15],
					legend: {},
					xAxis: {
						disableGrid: true
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						},
						area: {
							gradient: true
						}
					}
				}
			};
		},
		mounted() {
			this.getServerData();
		},
		onLoad: function(option) {
			console.log("上个页面穿啊来的", option.childrenId)
			if (option.childrenId) {
				this.childrenId = option.childrenId
			} else {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			}

		},
		methods: {
			gotoWeight() {
				uni.redirectTo({
					url: "/pages/babyWeight/babyWeight?childrenId=6"
				})
			},
			gotoAdd() {
				uni.navigateTo({
					url: "/pages/addHeightWeight/addHeightWeight?childrenId=6"
				})
			},
			showBack() {
				uni.redirectTo({
					url: "/pages/index/index"
				})
			},
			chooseQuxian() {
				this.chooseItem = !this.chooseItem
			},
			getServerData() {
				this.token = uni.getStorageSync("Authorization")
				uni.request({
					url: this.$serverUrl + '/api/height?childrenId=' + 6 + '&pageSize=' + this
						.pageSize + '',
					method: 'GET',
					header: {
						'Authorization': this.token
					},
					success: (res) => {
						console.log("初始化数据", res)
						if (res.statusCode == 200) {
							if (res.data.code == 1) {
								const pageData = res.data.data
								this.heightRecords = pageData.records
								this.initCharts()
							} else {
								console.log("error", res.data)
							}
						}
					},
					fail: (error) => {
						console.log("error", error)
					}
				})

			},
			initCharts() {
				let heightLength = this.heightRecords.length
				let cats = [],
					jilu = []
				for (var i = 0; i < heightLength; i++) {
					cats.push(this.heightRecords[i].cdAge)
					jilu.push(this.heightRecords[i].cdHeight)
				}
				let res = {
					categories: cats,
					series: [{
						pointShape: "square",
						name: "宝宝身高记录",
						data: jilu
					}]
				};
				this.chartData = JSON.parse(JSON.stringify(res));
			}
		}
	}
</script>

<style>
	.tianjia {
		color: white;
		background-color: #666ee8;
		border-radius: 50rpx;
		margin-left: 50rpx;
		margin-right: 50rpx;
		margin-bottom: 50rpx;
	}

	.tubiao {
		margin-top: 100rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		padding: 50rpx;
		box-shadow: 2px 2px 5px #9d9fa7, 2px 2px 5px #9d9fa7, 2px 2px 5px #9d9fa7, 2px 2px 5px #9d9fa7;
	}

	.tubiao2 {
		margin-top: 100rpx;
		margin-left: 30rpx;
		padding: 50rpx;
	}

	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		display: flex;
		margin-top: 10rpx;
		width: 100%;
		height: 600rpx;
	}

	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box2 {
		display: flex;
		flex-direction: column;
		margin-top: 10rpx;
		width: 100%;
	}

	.quxian {
		line-height: 70rpx;
		height: 70rpx;
		color: #9d9fa7;
		font-size: 33rpx;
		width: 50%;
		border-top-left-radius: 50rpx;
		border-bottom-left-radius: 50rpx;
	}

	.jilu {
		line-height: 70rpx;
		height: 70rpx;
		color: #9d9fa7;
		font-size: 33rpx;
		width: 50%;
		border-top-right-radius: 50rpx;
		border-bottom-right-radius: 50rpx;
	}

	.choose-item {
		color: white;
		background-color: #666ee8;
	}

	.city {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin-left: 4px;
	}

	.xuanxiang {
		display: flex;
		width: 70%;
		height: 70rpx;
		border-radius: 50rpx;
		border: 1rpx solid #eaeef0;
		align-items: center;
		text-align: center;
	}
</style>
